package indi.kider.server.applet.api;

import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.apartment.HouseApplet;
import indi.kider.server.applet.model.User;
import indi.kider.server.applet.service.CommService;
import indi.kider.server.applet.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    private CommService commService;

    @GetMapping("app-house/getCommunity")
    public CommonResponse<List<Community>> getCommunity() {
        try {
            return CommonResponse.buildGetSuccessResponse(houseService.getCommunity());
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-house/getHouse")
    public CommonResponse<List<Map>> getHouse(@RequestBody HouseApplet houseApplet, HttpServletRequest request) {
        try {
            User user  = commService.getCurrentUser(request,new User());
            return CommonResponse.buildGetSuccessResponse(houseService.getHouse(houseApplet,user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-house/houseIn")
    public CommonResponse<List<Map>> houseIn(@RequestBody HouseApplet houseApplet, HttpServletRequest request) {
        try {
            User user  = commService.getCurrentUser(request,new User());
            return CommonResponse.buildGetSuccessResponse(houseService.houseIn(houseApplet,user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-house/houseInList")
    public CommonResponse<List<Map>> houseInList(HttpServletRequest request) {
        try {
            User user  = commService.getCurrentUser(request,new User());
            return CommonResponse.buildGetSuccessResponse(houseService.houseInList(user));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    @PostMapping("app-house/sameTenant")
    public CommonResponse<List<Map>> sameTenantList(@RequestBody HouseApplet houseApplet, HttpServletRequest request) {
        try {
            User user  = commService.getCurrentUser(request,new User());
            return CommonResponse.buildGetSuccessResponse(houseService.getSameHouseTenantInfoList(houseApplet));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

}
